//
//  HACBrushAxis.m
//  HStockCharts
//
//  Created by Hotacool on 2018/9/14.
//

#import "HACBrushAxis.h"
#import "HACAxisProvider.h"
#import "HACBasicComponents.h"
#import "HACPainter.h"
@implementation HACBrushAxis {
    NSMutableArray<HACPathPainter*> *_linePainterList;
    NSMutableArray<HACTextPainter*> *_textPainterList;
}

- (instancetype)initWithDataProvider:(id<HACChartDataProvider>)provider {
    if (self = [super initWithDataProvider:provider]) {
        _linePainterList = [NSMutableArray array];
        _textPainterList = [NSMutableArray array];
    }
    return self;
}

- (HACPathPainter*)getLinePainterWithIndex:(NSInteger)index {
    @synchronized(self){
        if (_linePainterList.count > index) {
            return [_linePainterList[index] clearContents];
        } else {
            HACPathPainter *player = [HACPathPainter painterLayer];
            [_linePainterList addObject:player];
            return player;
        }
    }
}

- (HACTextPainter*)getTextPainterWithIndex:(NSInteger)index {
    @synchronized(self){
        if (_textPainterList.count > index) {
            return _textPainterList[index];
        } else {
            HACTextPainter *player = [HACTextPainter painterLayer];
            [_textPainterList addObject:player];
            return player;
        }
    }
}

- (void)clean {
    [_linePainterList enumerateObjectsUsingBlock:^(HACPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj clearContents];
    }];
    [_textPainterList enumerateObjectsUsingBlock:^(HACTextPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.string = nil;
    }];
}

- (void)removeBrush {
    [_linePainterList enumerateObjectsUsingBlock:^(HACPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
    [_textPainterList enumerateObjectsUsingBlock:^(HACTextPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
}

- (void)drawInLayer:(CALayer *)layer inRect:(CGRect)rect {
    [super drawInLayer:layer inRect:rect];
    
    DISABLE_ANIMATION_BEIGIN
    NSArray *datas;
    if ([self.provider conformsToProtocol:@protocol(HACAxisProvider)]) {
        id<HACAxisProvider> provider = (id<HACAxisProvider>)self.provider;
        datas = [provider axisDatasInRect:rect];
    }
    [_linePainterList enumerateObjectsUsingBlock:^(HACPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[obj clearContents] removeFromSuperlayer];
    }];
    [_textPainterList enumerateObjectsUsingBlock:^(HACTextPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
    int idx = 0;
    int idx_t = 0;
    for (HACAxisComponents *axisItem in datas) {
        // line
        HACPathPainter *player = [self getLinePainterWithIndex:idx++];
        player.zPosition = 100;
        player.strokeColor = axisItem.lineColor.CGColor;
        player.lineWidth = axisItem.lineBolder;
        player.fillColor = nil;
        player.anchorPoint = CGPointMake(0, 0);
        if (axisItem.dashLengths) { // 如果需要虚线
            player.lineDashPattern = axisItem.dashLengths;
        }
        __weak typeof(self) weakSelf = self;
        [player generateSubPath:^UIBezierPath *{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            return [strongSelf _generatePathFromPoints:@[NSStringFromCGPoint(axisItem.line.start),
                                                         NSStringFromCGPoint(axisItem.line.end)]
                                              isSmooth:NO];
        }];
        [player paintInLayer:layer];
        HACTextPainter *tlayer;
        CGSize size;
        //text
        NSString *textStr = axisItem.text;
        if (textStr && textStr.length > 0) {
            tlayer = [self getTextPainterWithIndex:idx_t++];
            tlayer.zPosition = 999;
            tlayer.anchorPoint = CGPointMake(0, 0);
            tlayer.foregroundColor = axisItem.textColor.CGColor;
            tlayer.string = textStr;
            tlayer.font = (__bridge CFTypeRef _Nullable)(axisItem.textFont);
            tlayer.fontSize = kAxisTextFontSize;
            size = [textStr boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : axisItem.textFont} context:nil].size;
            tlayer.bounds = (CGRect){{0,0},size};
            tlayer.position = axisItem.textFrame.origin;
            tlayer.contentsScale = [UIScreen mainScreen].scale;// 这句话使得字体不模糊,这是因为屏幕的分辨率问题,参照图片@1x. @2x. @3x .改为相应数字就可以
            [tlayer paintInLayer:layer];
        }
        //ext text
        textStr = axisItem.extText;
        if (textStr && textStr.length > 0) {
            tlayer = [self getTextPainterWithIndex:idx_t++];
            tlayer.zPosition = 999;
            tlayer.anchorPoint = CGPointMake(0, 0);
            tlayer.foregroundColor = axisItem.extTextColor.CGColor;
            tlayer.string = textStr;
            tlayer.font = (__bridge CFTypeRef _Nullable)(axisItem.extTextFont);
            tlayer.fontSize = kAxisTextFontSize;
            size = [textStr boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : axisItem.extTextFont} context:nil].size;
            tlayer.bounds = (CGRect){{0,0},size};
            tlayer.position = axisItem.extTextFrame.origin;
            tlayer.contentsScale = [UIScreen mainScreen].scale;// 这句话使得字体不模糊,这是因为屏幕的分辨率问题,参照图片@1x. @2x. @3x .改为相应数字就可以
            [tlayer paintInLayer:layer];
        }
    }
    DISABLE_ANIMATION_END
}
@end
